/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package tal.drivechain.wildcard;

import tal.drivechain.chain.ChainNode;
import tal.drivechain.system.Version;
import tal.drivechain.wildcard.Wildcard.Operator;

/**
 *
 * @author Main
 */
public class VersionElement implements WildcardElement {
    Version version;
    
    public void parse(String element) {
        version = Version.parseVersion(element, null);
    }

    public boolean matches(Object value, ChainNode context) {
        return (((Double)value).doubleValue()==version.getVersionLevel());
    }

    public boolean validOperator(Operator opcode) {
        switch (opcode) {
            case AND:
            case ANY:
            case NOT:
            case ELEMENT:
            case OR:
            case RANGE:
                return true;
            default:
                return false;
        }
    }

    public WildcardElement makeWildcardElement(String element) {
        VersionElement ret = new VersionElement();
        ret.parse(element);
        return ret;
    }

    public Object getValue(ChainNode context) {
        return version.getVersionLevel();
    }

    public boolean matchesElementFormat(String wc) {
        try {
            Version.parseVersion(wc, "");
            return true;
        } catch (IllegalArgumentException ie) { return false; }
    }

    public String toString(ChainNode context) {
        return version.toString(false, true, true, false);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof VersionElement) {
            if (super.equals(obj)) return true;
            else {
                VersionElement that = (VersionElement)obj;
                return (this.version.equals(that.version));
            }
        } else return false;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 71 * hash + (this.version != null ? this.version.hashCode() : 0);
        return hash;
    }
}
